Public Class COBAS400
'server
#Region "Structures"
    Public Structure Patient
        Dim SID As String
        Dim TestCode As String
        Dim DataValue As String
        Dim DateTestCompleted As String
    End Structure
#End Region
    Public Const SOH As Byte = &H1
    Public Const Intergra400_InstCode As String = "14"
    Public Const Intergra400_ID As String = "COBAS INTEGRA400"
    Public Const IdleCode As String = "00"
    Public Const ResultRequestCode As String = "09"
    Class BlockHeader
        Private strInstrumentCode As String
        Private strInstrumentIdentifier As String
        Private strBlockCode As String
        Function CreateBlockHeader() As String
            Dim strBH As String = String.Concat(InstrumentCode, " ", InstrumentIdentifier, " ", BlockCode, vbLf)
            Return strBH
        End Function
        Property BlockCode() As String
            Get
                Return strBlockCode
            End Get
            Set(ByVal value As String)
                strBlockCode = value
            End Set
        End Property
        Property InstrumentCode() As String
            Get
                Return strInstrumentCode
            End Get
            Set(ByVal value As String)
                strInstrumentCode = value
            End Set
        End Property
        Property InstrumentIdentifier() As String
            Get
                Return strInstrumentIdentifier
            End Get
            Set(ByVal value As String)
                strInstrumentIdentifier = value
            End Set
        End Property
    End Class
    Function CreateSyncBlock() As String
        Dim strSync As String
        Dim clsBlockHeader As New BlockHeader
        clsBlockHeader.InstrumentCode = Intergra400_InstCode
        clsBlockHeader.InstrumentIdentifier = Intergra400_ID
        clsBlockHeader.BlockCode = IdleCode
        strSync = String.Concat(Chr(SOH), vbLf, clsBlockHeader.CreateBlockHeader, Chr(STX), vbLf, Chr(ETX), vbLf, Chr(EOT), vbLf)
        Return strSync
    End Function
    Function CreateResultRequest() As String
        Dim strResultReq As String
        Dim strLineReq As String = "10 07"
        Dim clsBlockHeader As New BlockHeader
        clsBlockHeader.InstrumentCode = Intergra400_InstCode
        clsBlockHeader.InstrumentIdentifier = Intergra400_ID
        clsBlockHeader.BlockCode = ResultRequestCode
		CurrentSC = (CurrentSC + 1) Mod 2
        strResultReq = String.Concat(Chr(SOH), vbLf, clsBlockHeader.CreateBlockHeader, Chr(STX), vbLf, strLineReq, vbLf, Chr(ETX), vbLf, Str(CurrentSC).Trim, vbLf)
        Dim strBC As String = Left(Str(StringChecksum(strResultReq)).Trim, 3)



        strResultReq = String.Concat(strResultReq, strBC, vbLf, Chr(EOT), vbLf)
        Return strResultReq
    End Function
    Function ResultParse(ByVal strUnparsed As String, ByRef structPatient As Patient) As Boolean
        Dim UnparsedArray() As String = strUnparsed.Split(vbLf)
        If UnparsedArray(1).Substring(20, 2) <> "04" Then
            Return False
        End If
        Try
            For i As Byte = 3 To UnparsedArray.Length - 1
                If UnparsedArray(i) = Chr(ETX) Then
                    Exit For
                End If
                Dim LC As String = UnparsedArray(i).Substring(0, 2)
                Select Case LC
                    Case "53"
                        structPatient.SID = UnparsedArray(i).Substring(3, 15).Trim
                        structPatient.DateTestCompleted = UnparsedArray(i).Substring(19, 10)
                        If structPatient.DateTestCompleted = "00/00/0000" Then
                            structPatient.DateTestCompleted = Format(Now(), "dd/MM/yyyy")
                        End If
                    Case "55"
                        structPatient.TestCode = UnparsedArray(i).Substring(3, 3).Trim
                    Case "00"
                        structPatient.DataValue = Format(Val(UnparsedArray(i).Substring(3, 13).Trim), "n")
                End Select
            Next
            Return True
        Catch ex As Exception
            Return False
        End Try
    End Function
    Private Function StringChecksum(ByVal sInput As String) As Integer
        Dim bSTemp() As Char
        Dim bChecksum As Int16 = 0
        bSTemp = sInput.ToCharArray()
        For Each CharinArr As Char In bSTemp
            bChecksum += Asc(CharinArr)
            bChecksum = bChecksum Mod 1000
        Next
        Return bChecksum
    End Function
End Class
